package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.CommentMode;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.CommentContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 磁磁帅 on 2018/4/14.
 * 功能：
 */

public class CommentPresenter extends BaseContract.Presenter<CommentContract.View> {
    public CommentPresenter(CommentContract.View baseView) {
        super(baseView);
    }

    public void getCommentList(String id, int page) {
        Map<String, Object> map = new HashMap<>();
        //map.put("page", page);
        map.put("documentID", id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetCommentList, new BaseRequestBack<BaseRet<List<CommentMode>>>() {
                    public BaseRet<List<CommentMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<CommentMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<CommentMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<CommentMode>> ret) {
                        if (baseView != null) {
                            if (ret.isOk()) {
                                baseView.getCommentListRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.applyError(retStr);
                        }
                    }
                });
    }

    public void sendComment(String documentID, String content) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        new HttpClient().setHttpType(HttpClient.HttpType.JSON)
                .addParam("comment", content)
                .addParam("userID", mode.id)
                .addParam("userName", mode.userName)
                .addParam("documentID", documentID)
                .httpRequest(a.SendComment, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, BaseRet.class);
                    }

                    public void onResponseSucc(BaseRet ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.sendCommentSucc();
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
